﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SF.Base.Core;
using System.Text.RegularExpressions;

namespace SF.Dict.Web
{
    public partial class Register : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            string Account = tb_account.Text.Trim();
            string Password = tb_password.Text.Trim();
            string RePassword = tb_repassword.Text.Trim();

            if (string.IsNullOrEmpty(Account))
            {
                hf_error.Value = "E160207";
                return;
            }

            if (string.IsNullOrEmpty(Account))
            {
                hf_error.Value = "E160207";
                return;
            }

            Regex reg = new Regex("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$");

            if (!reg.IsMatch(Account))
            {
                hf_error.Value = "E160212";
                return;
            }

            if (string.IsNullOrEmpty(Password))
            {
                hf_error.Value = "E160208";
                return;
            }

            if (Password.Length < 6)
            {
                hf_error.Value = "E160210";
                return;
            }

            if (string.IsNullOrEmpty(RePassword))
            {
                hf_error.Value = "E160209";
                return;
            }

            if (RePassword != Password)
            {
                hf_error.Value = "E160211";
                return;
            }

			UserEntity User = UserRules.GetUserByLogin(Account);

            if (User != null)
            {
                hf_error.Value = "E160206";
                return;
            }

			int ret = UserRules.RegisterUser(Account, Password);

            if(ret < 0 )
            {
                hf_error.Value = "E160206";
                return;
            }

			Global.DTUser = UserRules.GetUserByLogin(Account);

			MetaRules.AddUserMeta(Global.DTUser.UserId, "PROFILEIMAGEURL", "http://tp1.sinaimg.cn/1558226504/50/1298392557/1");

            Response.Redirect("/Home.aspx");
        }
    }
}